
//
//  MainViewController.swift
//  FWSinaWeibo
//
//  Created by 范伟 on 16/2/21.
//  Copyright © 2016年 FanWei. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
        setupTabBar()
        showNewFeatureView()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.bringSubviewToFront(composeButton)
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count) - 1
//        Returns a rectangle that is smaller or larger than the source rectangle, with the same center point.
        composeButton.frame = CGRectInset(tabBar.bounds, width * 2, 0)
    }
    
    private lazy var composeButton: UIButton = UIButton(
        fw_title: nil,
        imageName:"tabbar_compose_icon_add",
        backgroundImageName: "tabbar_compose_button")
    
    @objc private func didClickComposeButton() {
        print("点击了")
    }
}
// MARK: - 新特性处理
extension MainViewController {
    private func showNewFeatureView() {
        //用户没有登陆则什么也不做
        if !UserAccount.sharedInstance.isLogin {
            return
        }
        //判断是否有新版本，如果是新版本则转至新特性界面，否则转至欢迎界面
        let v = isNewVersion ? NewFeatureView() : WelcomeView()
        //将目标界面添加到当前view中
        view.addSubview(v)
    }
    private var isNewVersion: Bool {
        //当前版本号
        let currentVersion = NSBundle.fw_appCurrentVersion
        let appVersionKey = "cn.itcast.appVersionKey"
        let defaults = NSUserDefaults.standardUserDefaults()
        //先获取当前沙盒路径下的应用版本号
        let sandboxVersion = defaults.stringForKey(appVersionKey)
        //保存当前版本号至沙盒路径
        defaults.setObject(currentVersion, forKey: appVersionKey)
        defaults.synchronize()
        NSLog(currentVersion)
//        return currentVersion.compare(sandboxVersion) == NSComparisonResult.OrderedDescending
//        return currentVersion > sandboxVersion
        return currentVersion == sandboxVersion
    }
    
}
extension MainViewController {
    private func setupTabBar() {
        //提供的图像会自动拉伸
        tabBar.shadowImage = UIImage.fw_singleDotImage(UIColor(white: 0.9, alpha: 1.0))
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        tabBar.addSubview(composeButton)
        composeButton.addTarget(self, action: "didClickComposeButton", forControlEvents: .TouchUpInside)
    }
}

extension MainViewController {
    private func addChildViewControllers() {
        addChildViewController(HomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageViewController(), title: "消息", imageName: "tabbar_message_center")
        //添加一个空白的视图控制器
        addChildViewController(UIViewController())
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        vc.title = title//由内向外层设置title
        //设置tabBar图片
        vc.tabBarItem.image = UIImage(named: imageName)
        //此处字典中的键定义在UIKit框架NSAttributedString.h中
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(13)], forState: .Normal)
        
        //tabBar选中状态的图片
//        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")
        //不再应用系统自动渲染样式而是使用素材进行渲染
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        let nav = UINavigationController(rootViewController: vc)
        //Adds the specified view controller as a child of the current view controller.
        addChildViewController(nav)
    }
}